/*
 * @Author: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @Date: 2025-10-02 19:47:27
 * @LastEditors: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @LastEditTime: 2025-10-02 20:14:56
 * @FilePath: /GateServer/src/util/config.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include"config.h"
#include "logger.h"
#include <list>
#include <sstream>
#include <utility>
#include <yaml-cpp/node/node.h>
namespace Yizhi {

    

    ConfigVarBase::ptr ConfigMgr::LookupBase(const std::string &name){
        auto it=Getdatas().find(name);
        return it->first.empty()?nullptr:it->second;
    }


    void ListAllMember(YAML::Node &node,const std::string prefix,std::list<std::pair<std::string,YAML::Node >>& output){
        if(prefix.find_first_not_of("abcdefjhijklmnopqrstuvwxyz._0123456789")!=std::string::npos){
            LOG_ERROR("prefix vaild");
            return;
        }
        output.push_back(std::make_pair(prefix,node));
        if(node.IsMap()){
            for(auto it=node.begin();it!=node.end();it++){
                ListAllMember(it->second,prefix.empty()?it->first.Scalar():prefix+"."+it->first.Scalar(), output);
            }
        }
    }


    void ConfigMgr::LoadFromYaml(YAML::Node &node){
        std::list<std::pair<std::string,YAML::Node>>allnodes;
        ListAllMember(node,"",allnodes);
        for(auto it:allnodes){
            const std::string key=it.first;
            if(key.empty()){
                continue;
            }

            ConfigVarBase::ptr var=LookupBase(key);
            if(var){
                if(it.second.IsScalar()){
                    var->Fromstring(it.second.Scalar());
                }
                else {
                    std::stringstream ss;
                    ss<<it.second;
                    var->Fromstring(ss.str());
                }
            }
        }
    }

}